import java.util.Stack;

public class 字符串解码 {

    public String decodeString(String ss) {
        StringBuilder sb = new StringBuilder();
        char[] s = ss.toCharArray();
        Stack<Integer> stackNum = new Stack<>();
        Stack<String> stackStr = new Stack<>();
        int multiply = 0;
        for (char ch : s) {
            if (ch >= '0' && ch <= '9') {
                // 如果是数字, 就进行存储
                multiply = multiply * 10 + (ch - '0');
            } else if (ch == '[') {
                stackNum.push(multiply);
                stackStr.push(sb.toString());
                multiply = 0;
                sb = new StringBuilder();
            } else if (ch == ']') {
                StringBuilder tmp = new StringBuilder();
                int x = stackNum.pop();
                for (int i = 0; i < x; i++) {
                    tmp.append(sb.toString());
                }
                sb = new StringBuilder(stackStr.pop() + tmp);
            } else {
                sb.append(ch);
            }
        }
        return sb.toString();
    }

}
